﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using DTC.Database;

namespace DTC.Popups
{
    /// <summary>
    /// Interaction logic for ParentPopup.xaml
    /// </summary>
    public partial class ParentPopup : Window
    {
        public ParentPopup()
        {
            InitializeComponent();
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            parent = new DTCParent();
            MainSP.DataContext = parent;
            isNew = true;
        }
        bool isNew;

        private ObservableCollection<DTCPhone> phones;

        private DTCParent parent;
        public DTCParent DParent
        {
            get
            {
                return parent;
            }
            set
            {
                parent = value;                
                MainSP.DataContext = parent;
                phones = new ObservableCollection<DTCPhone>(parent.DTCPhones.ToList());
                PhonesListBox.ItemsSource = phones;
                isNew = false;
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (DialogResult == true)
            {
                if (phones != null)
                    foreach (DTCPhone ph in phones)
                    {
                        if (PhoneNeedsToAdd(ph))
                        {
                            if (parent.DTCPhones == null)
                                parent.DTCPhones = new System.Data.Linq.EntitySet<DTCPhone>();
                            parent.DTCPhones.Add(ph);
                        }
                    }
                if (isNew)
                {
                    parent.Surname = SurnameTB.Text;
                    parent.Name = NameTB.Text;
                    parent.Fathername = FathernameTB.Text;
                    parent.Status = StatusTB.Text;
                    parent.WorkOrganisation = WorkOrganisationTB.Text;
                    parent.Occupation = OccupationTB.Text;
                    parent.Insert();
                }
                else
                {
                    bool NeedWrite = false;
                    if (SurnameTB.Text != parent.Surname)
                    {
                        parent.Surname = SurnameTB.Text;
                        NeedWrite = true;
                    }
                    if (NameTB.Text != parent.Name)
                    {
                        parent.Name = NameTB.Text;
                        NeedWrite = true;
                    }
                    if (FathernameTB.Text != parent.Fathername)
                    {
                        parent.Fathername = FathernameTB.Text;
                        NeedWrite = true;
                    }
                    if (StatusTB.Text != parent.Status)
                    {
                        parent.Status = StatusTB.Text;
                        NeedWrite = true;
                    }
                    if (WorkOrganisationTB.Text != parent.WorkOrganisation)
                    {
                        parent.WorkOrganisation = WorkOrganisationTB.Text;
                        NeedWrite = true;
                    }
                    if (OccupationTB.Text != parent.Occupation)
                    {
                        parent.Occupation = OccupationTB.Text;
                        NeedWrite = true;
                    }
                    //phones stuff
                    if (NeedWrite)
                        parent.Update();
                }
            }
        }

        private void NewPhoneButton_Click(object sender, RoutedEventArgs e)
        {
            PhonePopup newPhone = new PhonePopup();
            newPhone.Owner = this;
            if(newPhone.ShowDialog() == true)
            {
                if (phones == null)
                {
                    phones = new ObservableCollection<DTCPhone>();
                    PhonesListBox.ItemsSource = phones;
                }
                phones.Add(newPhone.Phone);
            }
        }

        private bool PhoneNeedsToAdd(DTCPhone pn)
        {

            if (parent.DTCPhones == null)
                return true;
            if(parent.DTCPhones.Contains(pn))
                    return false;
            return true;
        }

        private void DelPhoneButton_Click(object sender, RoutedEventArgs e)
        {
            if (PhonesListBox.SelectedIndex != -1)
            {
                DTCPhone phn = (DTCPhone)PhonesListBox.SelectedItem;
                ConfirmationPopup cp = new ConfirmationPopup();
                cp.TitleText = "Необходимо подтверждение";
                cp.Message = "Вы уверенны что хотите\nудалить телефон?";
                if (cp.ShowDialog() == true)
                {
                    PhonesListBox.SelectedIndex = -1;
                    phones.Remove(phn);
                    phn.Delete();
                }
            }
        }

        private void ViewPhoneButton_Click(object sender, RoutedEventArgs e)
        {
            if (PhonesListBox.SelectedIndex != -1)
            {
                PhonePopup pp = new PhonePopup();
                pp.Owner = this;
                pp.Phone = (DTCPhone)PhonesListBox.SelectedItem;
                pp.ShowDialog();
            }
        }

        private void PhonesListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (PhonesListBox.SelectedIndex != -1)
            {
                PhonePopup pp = new PhonePopup();
                pp.Owner = this;
                pp.Phone = (DTCPhone)PhonesListBox.SelectedItem;
                pp.ShowDialog();
            }
        }
    }
}
